//
//  DualMediationMAXRewardedVideoManager.h
//  DualMediationSDK
//
//  Created by YeJary on 2025/1/15.
//

#import <Foundation/Foundation.h>

@protocol DMAdDelegate;

#if __has_include(<UIKit/UIKit.h>)
#import <UIKit/UIKit.h>
#define UIKIT_AVAILABLE 1
#else
#define UIKIT_AVAILABLE 0
@class UIViewController;
#endif

#if __has_include(<AppLovinSDK/AppLovinSDK.h>)
#import <AppLovinSDK/AppLovinSDK.h>
#define APPLOVIN_SDK_AVAILABLE 1
#else
#define APPLOVIN_SDK_AVAILABLE 0
#endif

NS_ASSUME_NONNULL_BEGIN

@interface DualMediationMAXRewardedVideoManager : NSObject

@property (nonatomic, assign) id<DMAdDelegate> delegate;
@property (nonatomic, assign) BOOL isLoadRewardedVideo;
@property (nonatomic, assign, readonly) BOOL isUserClosed;

// 双广告位支持属性
@property (nonatomic, assign) BOOL isDualAdEnabled;
@property (nonatomic, strong) NSString *primaryAdUnitId;
@property (nonatomic, strong) NSString *secondaryAdUnitId;
@property (nonatomic, strong) NSString *currentAdUnitId;
@property (nonatomic, strong) NSString *currentPrimaryAdUnitId;
@property (nonatomic, strong) NSString *currentSecondaryAdUnitId;
@property (nonatomic, assign) BOOL isPrimaryAdLoaded;
@property (nonatomic, assign) BOOL isSecondaryAdLoaded;
@property (nonatomic, assign) NSTimeInterval primaryAdLoadTime;
@property (nonatomic, assign) NSTimeInterval secondaryAdLoadTime;
@property (nonatomic, assign) double primaryAdECPM;
@property (nonatomic, assign) double secondaryAdECPM;

// 重试机制相关属性
@property (nonatomic, assign) NSInteger retryCount;           // 当前重试次数
@property (nonatomic, assign) NSInteger maxRetryCount;        // 最大重试次数
@property (nonatomic, assign) NSTimeInterval baseRetryInterval; // 基础重试间隔（秒）
@property (nonatomic, assign) NSInteger totalFailureCount;    // 总失败次数
@property (nonatomic, assign) NSInteger totalLoadAttempts;    // 总加载尝试次数
@property (nonatomic, assign) NSTimeInterval lastRetryTime;   // 上次重试时间
@property (nonatomic, assign) BOOL isRetryPaused;            // 是否暂停重试
@property (nonatomic, strong) NSTimer *retryTimer;           // 重试定时器

+ (instancetype)sharedRewardedVideoManager;
- (void)setRewardedVideoDelegate;
- (void)loadRewardedVideo;
- (void)showRewardedVideo;
- (void)closeRewardedVideo;
- (void)resumeRewardedVideo;
- (void)checkPendingRewardedVideoDisplay;

// 场景化激励视频接口
- (void)showRewardedVideoForScene:(NSString *)scene;
- (BOOL)isRewardedVideoReadyForScene:(NSString *)scene;

// 双广告位支持方法
- (void)loadDualRewardedAds;
- (NSString *)selectOptimalAdUnitId;
- (void)switchToSecondaryAdIfNeeded;

// 重试机制相关方法
- (void)initializeRetrySettings;                    // 初始化重试设置
- (void)resetRetryState;                           // 重置重试状态
- (void)scheduleRetryWithDelay:(NSTimeInterval)delay; // 安排延迟重试
- (void)cancelRetryTimer;                          // 取消重试定时器
- (BOOL)shouldRetry;                               // 判断是否应该重试
- (NSTimeInterval)calculateRetryDelay;             // 计算重试延迟时间
- (void)updateFailureStatistics;                  // 更新失败统计
- (double)calculateFailureRate;                    // 计算失败率

@end

NS_ASSUME_NONNULL_END
